package com.syh.appshare.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.syh.appshare.Constants;
import com.syh.appshare.R;
import com.syh.appshare.objects.InstalledApkInfo;

public class Utils {

	private final static String tag="util";
	
	/**
	 * 
	 * @param context
	 * @param packageName
	 * @param className
	 */
	public static void notifyShare(Context context, String packageName,String className) {
		try {
			LogUtil.debug(tag, "notifyShare packageName:" + packageName+"className:"+className);
			NotificationManager nfm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			String appName=getAppName(context,packageName);
			String hint = context.getString(R.string.share_hint).replace("**", appName);
			String title = hint;
			String content=appName+" 版本号:"+getVersionName(context,packageName);
			Notification notification = new Notification(R.drawable.icon_small, hint,System.currentTimeMillis());
			notification.flags = Notification.FLAG_AUTO_CANCEL;// 点击自动清除通知

			Intent openintent = new Intent();
			openintent.setClassName(context.getPackageName(), "com.syh.appshare.modules.share.ShareDetailActivity");
			
			Bundle data = new Bundle();
			data.putString(Constants.PACKAGE_NAME_TAG, packageName);
			data.putString(Constants.CLASS_NAME_TAG, className);
			openintent.putExtras(data);
			PendingIntent contentIntent = PendingIntent.getActivity(context,Constants.SHARE_NOTIFY_TAG, openintent, 0);
			notification.setLatestEventInfo(context, title, content, contentIntent);
			nfm.notify(Constants.SHARE_NOTIFY_TAG, notification);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.error(tag, "notifyShare e:" + e.toString());
		}
	}
	
	/**
	 * 获取应用名称
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static String getAppName(Context context,String packageName){
		try {
			return context.getPackageManager().getPackageInfo(packageName, 0).applicationInfo.loadLabel(context.getPackageManager()).toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取VersionName
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static String getVersionName(Context context,String packageName){
		try {
			return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取已安装APK列表
	 * @param context
	 * @return
	 */
	public static List<InstalledApkInfo> getInstalledApkList(Context context){
		LinkedList<InstalledApkInfo> installedApkList = new LinkedList<InstalledApkInfo>();
		PackageManager pManager = context.getPackageManager();
		List<PackageInfo> appList = getAllInstalledApplications(context);

		Drawable apk_icon;
		String apk_ame;
		String apk_version;
		String packageName;
		String apk_version_code;// syh 2011-06-29
		boolean needUpdate;

		List<ResolveInfo> infoList = getLocalApps(context); // syh
		Map<String, String> infoMap = new HashMap<String, String>();

		for (int i = 0; i < infoList.size(); i++) {
			infoMap.put(infoList.get(i).activityInfo.packageName, infoList
					.get(i).activityInfo.name);
		}

		for (int i = 0; i < appList.size(); i++) {
			PackageInfo pinfo = appList.get(i);

			apk_icon = pManager.getApplicationIcon(pinfo.applicationInfo);
			apk_ame = pManager.getApplicationLabel(pinfo.applicationInfo).toString();
			apk_version = pinfo.versionName;
			packageName = pinfo.applicationInfo.packageName;
			apk_version_code = pinfo.versionCode + "";
			String className = infoMap.get(packageName);
			needUpdate = false;


			installedApkList.add(new InstalledApkInfo(apk_icon,apk_ame,
					apk_version, packageName, needUpdate, apk_version_code,
					className));
		}

		return installedApkList;
	}
	
	/**
	 * 获取已安装apk包信息
	 * @param context
	 * @return
	 */
	private static List<PackageInfo> getAllInstalledApplications(Context context) {

		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		PackageManager pm = context.getPackageManager();

		// 获取手机内所有应用
		List<PackageInfo> packageInfoList = pm.getInstalledPackages(0);
		for (int i = 0; i < packageInfoList.size(); i++) {
			PackageInfo packageInfo = (PackageInfo) packageInfoList.get(i);
			// 判断是否为非系统预装的应用程序
			if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				// 去除非本资源包的安装包
				if (!packageInfo.packageName.equalsIgnoreCase(context
						.getPackageName())) {
					apps.add(packageInfo);
				}
			}
		}
		return apps;
	}

	/**
	 * 获取已安装系统启动信息
	 * @param context
	 * @return
	 */
	private static List<ResolveInfo> getLocalApps(Context context) {
		List<ResolveInfo> mApps = new LinkedList<ResolveInfo>();
		Intent intent = new Intent(Intent.ACTION_MAIN, null);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		mApps = context.getPackageManager().queryIntentActivities(intent, 0);
		return mApps;
	}
}
